package cn.iocoder.lw.module.core.dal.mysql.house;

import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.lw.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.lw.module.core.controller.admin.house.vo.CoreHousePageReqVO;
import cn.iocoder.lw.module.core.dal.dataobject.house.CoreHouseDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 核心-房源 Mapper
 *
 * @author Lw源码
 */
@Mapper
public interface CoreHouseMapper extends BaseMapperX<CoreHouseDO> {

    default PageResult<CoreHouseDO> selectPage(CoreHousePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CoreHouseDO>()
                .eqIfPresent(CoreHouseDO::getProjectId, reqVO.getProjectId())
                .likeIfPresent(CoreHouseDO::getCommunityName, reqVO.getCommunityName())
                .likeIfPresent(CoreHouseDO::getBuilding, reqVO.getBuilding())
                .likeIfPresent(CoreHouseDO::getFloor, reqVO.getFloor())
                .likeIfPresent(CoreHouseDO::getRoomNumber, reqVO.getRoomNumber())
                .eqIfPresent(CoreHouseDO::getHouseType, reqVO.getHouseType())
                .eqIfPresent(CoreHouseDO::getStatus, reqVO.getStatus())
                .likeIfPresent(CoreHouseDO::getWaterMeterNumber, reqVO.getWaterMeterNumber())
                .likeIfPresent(CoreHouseDO::getElectricityMeterNumber, reqVO.getElectricityMeterNumber())
                .likeIfPresent(CoreHouseDO::getLockNumber, reqVO.getLockNumber())
                .likeIfPresent(CoreHouseDO::getContactName, reqVO.getContactName())
                .likeIfPresent(CoreHouseDO::getContactPhone, reqVO.getContactPhone())
                .betweenIfPresent(CoreHouseDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CoreHouseDO::getId));
    }

    default List<CoreHouseDO> selectListByProjectId(Long projectId) {
        return selectList(new LambdaQueryWrapperX<CoreHouseDO>()
                .eq(CoreHouseDO::getProjectId, projectId));
    }

    default List<CoreHouseDO> selectListByProjectIds(List<Long> projectIds) {
        return selectList(new LambdaQueryWrapperX<CoreHouseDO>()
                .in(CoreHouseDO::getProjectId, projectIds));
    }

    default List<CoreHouseDO> selectListByBuildingAndFloor(String building, String floor) {
        return selectList(new LambdaQueryWrapperX<CoreHouseDO>()
                .eq(CoreHouseDO::getBuilding, building)
                .eq(CoreHouseDO::getFloor, floor));
    }

    default List<CoreHouseDO> selectListByWaterMeterNumber(String waterMeterNumber) {
        return selectList(new LambdaQueryWrapperX<CoreHouseDO>()
                .eq(CoreHouseDO::getWaterMeterNumber, waterMeterNumber));
    }

    default List<CoreHouseDO> selectListByElectricityMeterNumber(String electricityMeterNumber) {
        return selectList(new LambdaQueryWrapperX<CoreHouseDO>()
                .eq(CoreHouseDO::getElectricityMeterNumber, electricityMeterNumber));
    }

    default List<CoreHouseDO> selectListByLockNumber(String lockNumber) {
        return selectList(new LambdaQueryWrapperX<CoreHouseDO>()
                .eq(CoreHouseDO::getLockNumber, lockNumber));
    }

    default long selectCountByProjectIdAndStatus(Long projectId, Integer status) {
        return selectCount(new LambdaQueryWrapperX<CoreHouseDO>()
                .eq(CoreHouseDO::getProjectId, projectId)
                .eqIfPresent(CoreHouseDO::getStatus, status));
    }


    /**
     * 批量更新房源状态
     *
     * @param ids 房源ID列表
     * @param status 房源状态
     * @return 更新数量
     */
    default int batchUpdateStatus(@Param("ids") List<Long> ids, @Param("status") Integer status) {
        if (ids == null || ids.isEmpty()) {
            return 0;
        }

        LambdaQueryWrapperX<CoreHouseDO> wrapper = new LambdaQueryWrapperX<CoreHouseDO>()
                .in(CoreHouseDO::getId, ids);

        CoreHouseDO updateDO = new CoreHouseDO();
        updateDO.setStatus(status);

        return update(updateDO, wrapper);
    }

}